package com.caigou.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jiliang.purchase.demo.PersonObject;
import jiliang.purchase.demo.think_liebiao;
import jiliang.purchase.demo.think_lingxing;
import jiliang.purchase.demo.think_shenqing;

import com.caigou.service.GoodsApplyService;


public class GoodsApplyServlet extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {	
		//System.out.println("OK");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession();
		//think_lingxi		
		String xiangmumc  = request.getParameter("xiangmumc");
		String lianxiren  = request.getParameter("lianxiren");
		String type = request.getParameter("type");
		String fuzerenxm_gh = request.getParameter("fuzerenxm");
		String fuzerenxm  = fuzerenxm_gh.split("\\-")[1];
		//fuzerenxm = new String(fuzerenxm.getBytes("ISO-8859-1"),"UTF-8");
		String fuzerengh  = fuzerenxm_gh.split("\\-")[0];
		String shiyongbm = request.getParameter("dw_id");
		String lianxidh  = request.getParameter("lianxidh");
		String gonghuosj  = request.getParameter("gonghuosj");
		String jingfeimc  = request.getParameter("jingfeimc");
		//jingfeimc = new String(jingfeimc.getBytes("ISO-8859-1"),"UTF-8");
		String jingfeidm  = request.getParameter("jingfeidm");
		String yusuanje  = request.getParameter("yusuanje");
		String[] caigoufs  = request.getParameterValues("caigoufs[]");
		String caigou = "";
		for(int i=0;i<caigoufs.length;i++)
		{
			caigou+=caigoufs[i]+",";
		}
		//System.out.println(caigou);
		//caigoufs = new String(caigoufs.getBytes("ISO-8859-1"),"UTF-8");
		long editsj = 0;

		String hejije = request.getParameter("hejije");
		String data = (new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());

		  try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date currentTime=new Date();
			  //将截取到的时间字符串转化为时间格式的字符串
			  Date beginTime=sdf.parse("1970-01-01 12:53:30");
			  //默认为毫秒
			  editsj=(currentTime.getTime()-beginTime.getTime()); 
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
 
		//System.out.println(editsj/1000);
		
		PersonObject PO = new PersonObject();
		PO = (PersonObject)session.getAttribute("Person");
		
		think_lingxing tlx = new think_lingxing(xiangmumc,lianxiren,fuzerengh,fuzerenxm,lianxidh,shiyongbm,gonghuosj,jingfeimc,jingfeidm,yusuanje,caigou, hejije,editsj/1000);
		think_shenqing tsq = new think_shenqing("4",PO.getGonghao(),PO.getXingming(),"申请人提交申请","22","1",data);
		List<think_liebiao> list_tlb = new ArrayList<think_liebiao>();
		
		
		//think_liebiao
		String num = request.getParameter("num");
		for(int i=1;i<=Integer.parseInt(num);i++)
		{
			if(!request.getParameter("mingcheng"+i).equals(""))
			{
				String mingcheng  = request.getParameter("mingcheng"+i);
				String miaoshu  = request.getParameter("miaoshu"+i);
				String danwei  = request.getParameter("danwei"+i);
				String shuliang = request.getParameter("shuliang"+i);
				String danjia  = request.getParameter("danjia"+i);
				String hejia  = request.getParameter("hejia"+i);
				String beizhu  = request.getParameter("beizhu"+i);
				think_liebiao tlb = new think_liebiao(mingcheng,miaoshu,danwei,shuliang,danjia,hejia,beizhu);
				list_tlb.add(tlb);
			}
			
		}
		 GoodsApplyService GAS = new GoodsApplyService();
		 
		 try {
			GAS.setNewApply(list_tlb, tlx,tsq,type);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.getRequestDispatcher("TheFirst.jsp").forward(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}

}
